package brickred.containers
{
    import flash.display.Stage;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Vector3D;

	import away3d.controllers.HoverController;

    import brickred.core.Director;
    import brickred.containers.Scene;
    import brickred.events.SceneEvent;

    public class HoverScene extends Scene
    {
        private var camera_controller_:HoverController;
        private var camera_controller_pan_angle_:Number;
        private var camera_controller_tilt_angle_:Number;
        private var camera_controller_distance_:Number;
        private var camera_controller_min_tilt_angle_:Number;
        private var camera_controller_max_tilt_angle_:Number;
		private var move_:Boolean = false;
		private var last_pan_angle_:Number;
		private var last_tilt_angle_:Number;
		private var last_mouse_x_:Number;
		private var last_mouse_y_:Number;

        public function HoverScene(pan_angle:Number = 0, tilt_angle:Number = 90,
            distance:Number = 1000, min_tilt_angle:Number = -90, max_tilt_angle:Number = 90)
        {
            camera_controller_pan_angle_ = pan_angle;
            camera_controller_tilt_angle_ = tilt_angle;
            camera_controller_distance_ = distance;
            camera_controller_min_tilt_angle_ = min_tilt_angle;
            camera_controller_max_tilt_angle_ = max_tilt_angle;
            addEventListener(SceneEvent.ADDED_TO_STAGE, onAddedToStage);
            addEventListener(SceneEvent.REMOVED_FROM_STAGE, onRemovedFromStage);
            addEventListener(SceneEvent.ENTER_FRAME, onEnterFrame);
        }

        private function onEnterFrame(event:SceneEvent):void
        {
            var stage:Stage = Director.getInstance().stage;

			if (move_) {
				camera_controller_.panAngle =
                    0.3 * (stage.mouseX - last_mouse_x_) + last_pan_angle_;
				camera_controller_.tiltAngle =
                    0.3 * (stage.mouseY - last_mouse_y_) + last_tilt_angle_;
			}
        }

        private function onAddedToStage(event:SceneEvent):void
        {
            var stage:Stage = Director.getInstance().stage;

            camera_controller_ = new HoverController(camera_, null,
                camera_controller_pan_angle_, camera_controller_tilt_angle_,
                camera_controller_distance_, camera_controller_min_tilt_angle_,
                camera_controller_max_tilt_angle_);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
        }

        private function onRemovedFromStage(event:SceneEvent):void
        {
            var stage:Stage = Director.getInstance().stage;

			stage.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
            camera_controller_ = null;
        }

		private function onMouseDown(event:MouseEvent):void
		{
            var stage:Stage = Director.getInstance().stage;

			last_pan_angle_ = camera_controller_.panAngle;
			last_tilt_angle_ = camera_controller_.tiltAngle;
			last_mouse_x_ = stage.mouseX;
			last_mouse_y_ = stage.mouseY;
			move_ = true;
			stage.addEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
		}
		
		private function onMouseUp(event:MouseEvent):void
		{
            var stage:Stage = Director.getInstance().stage;

			move_ = false;
			stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
		}
		
		private function onStageMouseLeave(event:Event):void
		{
            var stage:Stage = Director.getInstance().stage;

			move_ = false;
			stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
		}
    }
}

